//
//  XMBaseSettingController.m
//  XMWeibo
//
//  Created by chi on 14-9-1.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMBaseSettingController.h"




#import "XMSettingCell.h"

@interface XMBaseSettingController ()



@end

@interface XMBaseSettingController ()

@end

@implementation XMBaseSettingController


- (id)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {

            }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        self.tableView.backgroundColor = XMColor(219, 219, 219);

    self.dataList = [NSMutableArray array];
    
    // 设置属性
    if (isIOS7) {
        self.tableView.contentInset = UIEdgeInsetsMake(-25.0, 0, 0, 0);
    }

    
    self.tableView.sectionHeaderHeight= 5.0;
    self.tableView.sectionFooterHeight = 0.0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (XMGroupInfo *)addGroup
{
    XMGroupInfo *group = [XMGroupInfo group];
    [self.dataList addObject:group];
    
    return group;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    XMGroupInfo *groupInfo = self.dataList[section];
    
    return groupInfo.cellItems.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMSettingCell *cell = [XMSettingCell cellWithTableView:tableView indexPath:indexPath];
    
    XMGroupInfo *group = self.dataList[indexPath.section];
    
    XMBaseSettingItem *item = group.cellItems[indexPath.row];
    
    cell.cellItem = item;
    
    return cell;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    XMGroupInfo *group = self.dataList[section];
    return group.header;
}

- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    XMGroupInfo *group = self.dataList[section];
    return group.footer;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XMGroupInfo *group = self.dataList[indexPath.section];
    
    XMBaseSettingItem *settingItem = group.cellItems[indexPath.row];
    
    
    if (settingItem.operationBlock) {
        settingItem.operationBlock();
    }
    else if ([settingItem isKindOfClass:[XMCheckSettingItem class]]) {
        XMCheckGroupInfo *checkGroup = (XMCheckGroupInfo*)group;
        checkGroup.checkItem = (XMCheckSettingItem*)settingItem;
        
        [self.tableView reloadData];
    }
    else if ([settingItem isKindOfClass:[XMArrowSettingItem class]]) {
        XMArrowSettingItem *arrowSetting = (XMArrowSettingItem*)settingItem;
        
        if (arrowSetting.destVcClass) {
             UIViewController *controller = [[arrowSetting.destVcClass alloc]init];
            
            if (arrowSetting.readyForVc) {
                arrowSetting.readyForVc(arrowSetting, controller);
            }
            
            controller.navigationItem.title = arrowSetting.title;
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
    
}





@end
